package cn.bopomo.dealer.base.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.util.SecurityUtil;
import cn.bopomo.dealer.base.vo.OperatorDetail;
import cn.bopomo.dealer.base.vo.OperatorForm;

public class ModifyOperatorAction extends BaseAction {

	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		StringBuffer resultBuffer = new StringBuffer();
		
		OperatorForm operatorForm = (OperatorForm)form;
		
		
		//BeanUtils.copyProperties(operator,operatorForm);
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		OperatorDetail operator = os.getOperatorDetail(request.getParameter("id"));
		if (operator == null){
			resultBuffer.append("01").append("||").append(this.getCodeDescription("01",request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
			response.getWriter().write(resultBuffer.toString());
			return null;
		}
		operator.setLoginName(operatorForm.getLoginName());
		operator.setHasChild(operatorForm.getHasChild()? "1": "0");
		operator.setAuthorize(operatorForm.getAuthorize());
		operator.setIntroduction(operatorForm.getIntroduction());
		
		if (!"".equals(operatorForm.getPassword().trim()) && operatorForm.getPassword().length()>=6){
			operator.setPassword(SecurityUtil.MD5(operatorForm.getPassword()));
		}
		
		String result = os.modifyOperator(operator);
		
		resultBuffer.append(result).append("||").append(this.getCodeDescription(result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		response.getWriter().write(resultBuffer.toString());
		return null;
	}
}
